const db = uniCloud.database();
const uniID = require('uni-id-common')

exports.main = async (event, context) => {
	const uniIDIns = uniID.createInstance({ context });
	let body = event;
	if (event.body != undefined) {
		body = JSON.parse(event.body)
	}
	
  const { uid } = await uniIDIns.checkToken(body.uniIdToken);
  if (!uid) {
      return { code: 403, msg: "身份验证失败" };
  }

  try {
    const collection = db.collection("consultant_schedule");
    const result = await collection.where({ user: uid }).get();

let reservations = [];
result.data.forEach(record => {
  reservations.push({
    day: record.day,
    freeSlots: record.freeSlots || [],
	isShow: record.isShow
  });
});

    return { success: true, reservations };
  } catch (error) {
    console.error("数据库查询失败:", error);
    return { success: false, message: "数据库查询失败" };
  }
};
